BINPATH := ../bin/
CLIENT_EXECUTABLE := $(BINPATH)/client
SERVER_EXECUTABLE := $(BINPATH)/server
STATICLIB := $(BINPATH)/libweilin_net.a
SHAREDLIB := $(BINPATH)/libweilin_net.so

SRCDIRS := ./ ./base/ ./comm/ ./packet/ ./controller/ ./callback/ ./thread/ ./net/ ./rpcserver/ ./log/
OBJDIR = ../obj/
LIBPATHS := ../lib 
LIBS := boost_thread boost_system pthread rt glog
INCS := ../inc ../3rd 

CFLAGS := -g -Wall -fPIC

CC = g++
USER_MARCOS := _FILE_OFFSET_BITS=64

CPPSOURCE := $(foreach dir, $(SRCDIRS), $(wildcard $(dir)*.cpp))
CPPOBJS := $(patsubst %.cpp, $(OBJDIR)%.o, $(CPPSOURCE))

EXCLUDE_CPP := main_server.cpp main_client.cpp
EXCLUDE_OBJ := $(patsubst %.cpp,$(OBJDIR)./%.o,$(EXCLUDE_CPP))
OBJS_FILTER := $(filter-out $(EXCLUDE_OBJ), $(CPPOBJS))


help:
	@echo make client
	@echo make server
	@echo make staticlib
	@echo make sharedlib

client:$(CLIENT_EXECUTABLE)
$(OBJDIR)%.o:%.cpp
	$(CC) $(CFLAGS) $(addprefix -D,$(USER_MARCOS)) $(addprefix -I,$(INCS)) -c $< -o $@

$(CLIENT_EXECUTABLE):$(CPPOBJS) 
	$(CC) -o $@ $(OBJS_FILTER) $(OBJDIR)./main_client.o $(CFLAGS) $(addprefix -l, $(LIBS)) $(addprefix -L, $(LIBPATHS))


server:$(SERVER_EXECUTABLE)
$(OBJDIR)%.o:%.cpp
	$(CC) $(CFLAGS) $(addprefix -D,$(USER_MARCOS)) $(addprefix -I,$(INCS)) -c $< -o $@

$(SERVER_EXECUTABLE):$(CPPOBJS) 
	$(CC) -o $@ $(OBJS_FILTER) $(OBJDIR)./main_server.o $(CFLAGS) $(addprefix -l, $(LIBS)) $(addprefix -L, $(LIBPATHS))

staticlib:
	ar rcs $(STATICLIB) $(OBJS_FILTER)
	cp $(STATICLIB) /home/weilin/weilin_gate/lib/ -f
	cp $(STATICLIB) /home/weilin/weilin_server/lib/ -f
sharedlib:
	$(CC) $(CFLAGS) -o $(SHAREDLIB) -shared $(OBJS_FILTER) 
clean:
	rm -rf $(OBJDIR)
	rm -rf $(BINPATH)
	mkdir -p $(OBJDIR) $(OBJDIR)/base $(OBJDIR)/comm $(OBJDIR)/packet $(OBJDIR)/controller $(OBJDIR)/callback 
	mkdir -p $(OBJDIR)/log $(OBJDIR)/thread $(OBJDIR)/net $(OBJDIR)/rpcserver
	mkdir -p $(BINPATH)
